import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;

/**
 * Created with IntelliJ IDEA.
 * User: ttq
 * Date: 13-7-5
 * Time: 下午4:20
 * To change this template use File | Settings | File Templates.
 */
public class ImageUtil {
    public static byte[] waterMark(byte[] imageData, String waterMarkFile) throws IOException {
        int paddingRight = 10;
        int paddingBottom = 10;

        Image image = new ImageIcon(imageData).getImage();

        int imageWidth = image.getWidth(null);
        int imageHeight = image.getHeight(null);
        Image waterMark = ImageIO.read(new File(waterMarkFile));

        int waterMarkWidth = waterMark.getWidth(null);
        int waterMarkHeight = waterMark.getHeight(null);

        if (imageWidth < waterMarkWidth + 2 * paddingRight || imageHeight < waterMarkHeight + 2 * paddingBottom) {
            return imageData;
        }

        BufferedImage bufferedImage = new BufferedImage(imageWidth, imageHeight, BufferedImage.TYPE_INT_RGB);
        Graphics g = bufferedImage.createGraphics();

        g.drawImage(image, 0, 0, imageWidth, imageHeight, null);

        g.drawImage(waterMark, imageWidth - waterMarkWidth - paddingRight, imageHeight - waterMarkHeight - paddingBottom, waterMarkWidth, waterMarkHeight, null);
        g.dispose();
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(outputStream);

        encoder.encode(bufferedImage);
        byte[] data = outputStream.toByteArray();
        outputStream.close();
        return data;
    }
}
